// canvasEvent.js
class Event {
    private _listener: any;
    private _listeners: any;
  constructor () {
    this._listener = {}
  }

  /**
   * 监听
   */
  on (type:any, handler:any) {
    if (!this._listener[type]) {
      this._listener[type] = []
    }

    this._listener[type].push(handler)
  }

  /**
   *触发
   */
  emit (type:any, event:any) {
    if (event == null || event.type == null) {
        return
    }
    const typeListeners = this._listener[type]
    if (!typeListeners) return
    for (let index = 0; index < typeListeners.length; index++) {
      const handler = typeListeners[index]
      handler(event)
    }
  }

  /**
   * 删除
   */
  remove (type:any, handler:any) {
    if (!handler) {
      this._listener[type] = []
      return
    }

    if (this._listener[type]) {
      const listeners = this._listeners[type]
      for (let i = 0, len = listeners.length; i < len; i++) {
          if (listeners[i] === handler) {
              listeners.splice(i, 1)
          }
      }
    }
  }
}
export default Event
